var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address_list: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var page = this;
        wx.showNavigationBarLoading();
        util.request(api.AddressList).then(function(res){
          wx.hideNavigationBarLoading();
          if (res.errno == 0) {
            page.setData({
              addressList: res.data,
            });
          }
        });
         
    },

    pickAddress: function (e) {
        var page = this;
        var index = e.currentTarget.dataset.index;
        var address = page.data.addressList[index];
        wx.setStorageSync("picker_address", address);
        wx.navigateBack();
    },

    getWechatAddress: function (e) {
        var page = this;
        wx.chooseAddress({
            success: function (e) {
                if (e.errMsg != 'chooseAddress:ok')
                    return;
                wx.showLoading();
                util.request(api.AddressSave, {
                  national_code: e.nationalCode,
                  name: e.userName,
                  mobile: e.telNumber,
                  detail: e.detailInfo},'post').then(function(res){
                    if (res.errno == 1) {
                      wx.showToast({
                        title: res.msg,
                        image: "/images/icon-warning.png",
                      });
                      return;
                    }
                    if (res.errno == 0) {
                      wx.setStorageSync("picker_address", res.data);
                      wx.navigateBack();
                    }
                    wx.hideLoading();
                  });
            }
        });
    },
});